<template>
  <el-dialog
    v-model="dialogVisible"
    title="评论详情"
    width="70%"
    :before-close="handleClose"
  >
    <div v-loading="loading" class="comment-detail">
      <div v-if="commentData" class="detail-content">
        <!-- 基本信息 -->
        <div class="info-section">
          <h3 class="section-title">基本信息</h3>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="评论ID">
              {{ commentData.id }}
            </el-descriptions-item>
            <el-descriptions-item label="评论用户">
              <div class="user-info">
                <el-avatar :src="commentData.userAvatar" :size="32">
                  {{ commentData.username?.charAt(0) }}
                </el-avatar>
                <div class="user-details">
                  <div class="username">{{ commentData.username }}</div>
                </div>
              </div>
            </el-descriptions-item>
            <el-descriptions-item label="评论层级">
              <el-tag v-if="commentData.parentId" type="info">
                回复评论
              </el-tag>
              <el-tag v-else type="success">
                顶级评论
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="创建时间">
              {{ commentData.createTime }}
            </el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 评论内容 -->
        <div class="info-section">
          <h3 class="section-title">评论内容</h3>
          <div class="content-container">
            <div class="content-text">
              {{ commentData.content }}
            </div>
          </div>
        </div>

        <!-- 评论对象信息 -->
        <div v-if="commentData.documentTitle || commentData.caseNumber" class="info-section">
          <h3 class="section-title">评论对象</h3>
          <div class="comment-target-info">
            <el-descriptions :column="1" border>
              <el-descriptions-item v-if="commentData.documentTitle" label="对象类型">
                <el-tag type="primary">法律文档</el-tag>
              </el-descriptions-item>
              <el-descriptions-item v-if="commentData.documentTitle" label="文档标题">
                {{ commentData.documentTitle }}
              </el-descriptions-item>
              <el-descriptions-item v-if="commentData.caseNumber" label="对象类型">
                <el-tag type="warning">案件</el-tag>
              </el-descriptions-item>
              <el-descriptions-item v-if="commentData.caseNumber" label="案件编号">
                {{ commentData.caseNumber }}
              </el-descriptions-item>
            </el-descriptions>
          </div>
        </div>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { getCommentDetail } from '../../../api/comment'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  commentId: {
    type: [Number, String],
    default: null
  }
})

const emit = defineEmits(['update:visible'])

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

// 加载状态
const loading = ref(false)

// 评论数据
const commentData = ref(null)

// 获取评论详情
const fetchCommentDetail = async () => {
  if (!props.commentId) return
  
  loading.value = true
  try {
    const response = await getCommentDetail(props.commentId)
    if (response.code === 200) {
      commentData.value = response.data
    } else {
      ElMessage.error(response.message || '获取评论详情失败')
    }
  } catch (error) {
    console.error('获取评论详情失败:', error)
    ElMessage.error('获取评论详情失败')
  } finally {
    loading.value = false
  }
}

// 关闭对话框
const handleClose = () => {
  commentData.value = null
  emit('update:visible', false)
}

// 监听commentId变化，获取详情
watch(() => props.commentId, (newId) => {
  if (newId && props.visible) {
    fetchCommentDetail()
  }
}, { immediate: true })

// 监听对话框打开
watch(() => props.visible, (visible) => {
  if (visible && props.commentId) {
    fetchCommentDetail()
  }
})
</script>

<style scoped>
.comment-detail {
  min-height: 300px;
}

.detail-content {
  padding: 0 10px;
}

.info-section {
  margin-bottom: 30px;
}

.section-title {
  margin: 0 0 15px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  border-left: 4px solid #409EFF;
  padding-left: 10px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.username {
  font-weight: 500;
  color: #333;
}

.content-container {
  border: 1px solid #e4e7ed;
  border-radius: 6px;
  background-color: #fafafa;
  padding: 20px;
  max-height: 400px;
  overflow-y: auto;
}

.content-text {
  line-height: 1.8;
  font-size: 14px;
  color: #333;
  white-space: pre-wrap;
  word-break: break-word;
}

.comment-target-info {
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
}

.dialog-footer {
  text-align: right;
}

:deep(.el-descriptions__label) {
  width: 120px;
  font-weight: 600;
}

:deep(.el-descriptions__content) {
  word-break: break-word;
}

/* 滚动条样式 */
.content-container::-webkit-scrollbar {
  width: 6px;
}

.content-container::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.content-container::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.content-container::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .detail-content {
    padding: 0 5px;
  }
  
  .content-container {
    padding: 15px;
  }
  
  .user-info {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  :deep(.el-descriptions) {
    --el-descriptions-item-bordered-label-background: #fafafa;
  }
}
</style>
